package com.carlinfo.shop.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.shop.common.util.ConstatFinalUtil;
import com.carlinfo.shop.common.util.PageInfoUtil;
import com.carlinfo.shop.users.dao.IAAdminsDao;
import com.carlinfo.shop.users.pojo.AAdmins;
import com.carlinfo.shop.users.service.IUsersService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("usersService")
public class UserServiceImpl implements IUsersService
{
	@Resource
	private IAAdminsDao adminsDao;

	
	/**
	 * {
			// 响应码
			"code":"0",
			// 响应信息
			"info":"",
			// 数据
			"data":
			{
				// 影响条数;sql执行以后对数据库的影响条数
				"effect":"",
				// id,保存的时候要主键
				"id":""
			}
		}
	 */
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		
		int res = this.adminsDao.saveOne(admins);
		
		/* 抛空指针,就是为了测试事务管理器 */
		/*String str = null ; 
		str.toString() ;*/
		
		if (res >0 )
		{
			JSONObject dataJSON = new JSONObject() ;
			dataJSON.put("effect", res);
			dataJSON.put("id", admins.getId());
			
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
		
	}

	@Override
	public JSONObject updateOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject() ; 
		/* 保存 */
		int res = this.adminsDao.updateOne(admins);
		
		/* 抛空指针,就是为了测试事务管理器 */
		/*String str = null ; 
		str.toString() ;*/
		
		if(res > 0 )
		{
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("effect", res);
			dataJSON.put("id", admins.getId());
			
			resultJSON.put("data", dataJSON);
			/* 成功 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap)
	{
		return this.adminsDao.findOne(condMap);
	}

	@Override
	public List<AAdmins> findCondListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 为关键字加上模糊匹配 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
			
		if(pageInfoUtil != null)
		{
			/* 分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AAdmins> adminsList = this.adminsDao.findList(condMap);
			/* 一定要把总记录数放到pageInfoUtil中 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return adminsList ; 
		}
		return this.adminsDao.findList(condMap);
	}

	@Override
	public JSONObject deleteOneAdminsService(Map<String, Object> condMap)
	{
		JSONObject resultJSON = new JSONObject();
		
		int res = this.adminsDao.deleteOne(condMap);
		
		if (res > 0 )
		{
			JSONObject dataJSON = new JSONObject() ;
			dataJSON.put("effect", res);
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
}
